
*** CONTROLS COLLECTED AT BIRTH

use "Data/NCDS/UKDA-5565-stata11/stata11/ncds0123.dta", clear
label variable n0region "0 Region at PMS (1958) - Birth"
qui ds, not(varl "ncdsid serial number"|varl "0 *" varl "0-*")
drop `r(varlist)'

* rename serial number
rename ncdsid  NCDSID  
merge 1:1 NCDSID using "Data/NCDS/UKDA-5565-stata11/stata11/ncds_pms_additionals.dta"


label define nym 0 "No" 1 "Yes" 99 "Missing", replace

rename SBNND SBNND_0

ge InPMS_0=n514!=.

* sex
rename n622 sex_0
replace sex_0=. if sex_0<0

* region at birth 
rename n0region region_0
recode region_0 (-2=.)
label define n0region  3 "E and W Riding" 99 "Missing", modify
label values region_0 n0region 

* mother's age at birth
rename n553 imotherage_0
replace imotherage_0=. if imotherage_0<0
winsor imotherage_0, generate(motherage_0) p($p_winsior)
drop imotherage_0
label variable motherage_0  "Mother's age" 
ge MissingMA_0=motherage_0==.
label variable MissingMA_0  "Missing mother's age" 
su motherage_0
replace motherage_0=r(mean) if motherage_0==.

* parity
rename n504 iparity_0
recode iparity_0 (-1=.) 
replace iparity_0=iparity_0-1
winsor iparity_0, generate(parity_0) p($p_winsior) 
drop  iparity_0
label variable parity_0  "Parity" 
ge MissingMP_0=parity_0==.
label variable MissingMP_0  "Missing parity" 
su parity_0
replace parity_0=r(mean) if parity_0==.

* when mother stopped working during pregnancy 
rename n542 motherstopped_0
recode motherstopped_0 (-1=99) (.=99)
label define n542 11 "No job"  99 "Missing", modify
label values motherstopped_0 n542

* mother's job 
rename n540 motherjob_0
recode motherjob_0 (-1 .= 99)  (1 2 3 4 5=1) (6 7 8 9 10 11 12=2) (13 14 15 16=3) (17 18=4) (20=5)

* replace with sc of prior job if didn't work during preg.
rename n539 motherjobp_0
recode motherjobp_0 (-1 .= 99) (1 2 3 4 5=1) (6 7 8 9 10 11 12=2) (13 14 15 16=3) (17 18=4) (20=5)
replace motherjob_0=motherjobp_0 if (motherjob_0==5|motherjob_0==99) & motherjobp_0!=99 & motherjobp_0!=5
drop motherjobp_0
label define mj 1 "I or II" 2 "IIIN or IIIM" 3 "IV" 4 "V" 5 "No job" 99 "Missing", replace
label values motherjob_0 mj 

* recode for consistency
replace motherstopped_0=11 if motherjob_0==5
replace motherjob_0=5 if motherstopped_0==11
replace motherstopped_0=10 if motherjob_0<5 & motherstopped_0==99

* mother smoker prior to pregnancy
ge mothersmoke_0=1 if (n502>1 & n502!=.)
replace mothersmoke_0=0 if n502==1
replace mothersmoke_0 = 99 if mothersmoke_0==.
label values mothersmoke_0 nym  

* mother's fathers's social-class when mother left school (measured at CM's birth)
rename n526 motherfsc_0 
recode motherfsc_0  (-1 1 10 11 = 99)
recode motherfsc_0 (2=1) (3=2) (4=3) (5=4) (6=5)
label define mfsc 1 "I" 2 "II" 3 "IIIN or IIIM" 4 "IV" 5 "V" 99 "Missing", replace
label values motherfsc_0 mfsc

* mother stayed in school beyond comp. sch leaving age
ge motherpostcompeduc_0=1 if n537==3|n537==4|n537==6
replace motherpostcompeduc_0=0 if motherpostcompeduc_0!=1 & n537!=. &  n537>0
replace motherpostcompeduc_0=99 if motherpostcompeduc_0==.
label values motherpostcompeduc_0 nym  

* mother married or in stable union at CM's birth
ge mothermarried_0=1 if (n545==2 |n545==3 |n545==4)
replace mothermarried_0=0 if n545>0 & n545!=. & mothermarried_0==.

* social class of mother's husband at CM's birth (students, retired etc classified as V), includes marriage info
rename n492 fathersc_0
recode  fathersc_0 (1 10 9 11=6) 
recode  fathersc_0 (2=1) (3=2) (4=3) (5=4) (6=5) (12=6) (-1=.)
replace fathersc_0=6 if mothermarried_0==0
drop mothermarried_0
recode  fathersc_0 (.=7)
label define fsc  1 "I" 2 "II" 3 "IIIN or IIIM" 4 "IV" 5 "V" 6 "Mother single" 7 "Missing", replace
label values fathersc_0 fsc

* mother's husband's age at CM's birth
rename n494 ifatherage_0
replace ifatherage_0=. if ifatherage_0<0
replace ifatherage_0=. if fathersc_0==6
winsor ifatherage_0, generate(fatherage_0) p($p_winsior)
label variable fatherage_0 "Mother's husband's age" 
drop  ifatherage_0
ge MissingFA_0=fatherage_0==.
label variable MissingFA_0 "Missing mother's husband's age"
su fatherage_0
replace fatherage_0=r(mean) if fatherage_0==.

* gestation duration (weeks)
rename n497 igestation_0
replace igestation_0=. if igestation_0<0
winsor igestation_0, generate(gestation_0) p($p_winsior)
replace gestation_0=gestation_0/7
label variable gestation_0 "Gestational duration" 
drop igestation_0
ge MissingGE_0=gestation_0==.
label variable MissingGE_0 "Missing gestation duration"
su gestation_0
replace gestation_0=r(mean) if gestation_0==.


* birthweight (lb)
rename n574 ibirthweight_0
replace  ibirthweight_0=. if  ibirthweight_0<0
winsor ibirthweight_0, generate(birthweight_0) p($p_winsior)
replace birthweight_0=birthweight_0/16
label variable birthweight_0 "Birthweight" 
drop ibirthweight_0
ge MissingBW_0=birthweight_0==.
label variable MissingBW_0 "Missing birthweight"
su birthweight_0
replace birthweight_0 =r(mean) if birthweight_0==.

label define n542  6 "29 to 30th week", modify

* Generate labeled indicators
local catvar motherstopped_0 mothersmoke_0 motherjob_0 motherfsc_0 motherpostcompeduc_0 region_0 fathersc_0
foreach r of local catvar{
	indicators `r'
}

keep NCDSID *_0* 

ge Male=sex_0==1 if sex_0!=.
label var Male "Cohort member is male"
sort NCDSID 

save scratchdata/ncds0123_controlsbirth.dta, replace



